package com.scenic.orderservice.controller;

import com.scenic.common.bean.Cart;
import com.scenic.common.bean.RespBean;
import com.scenic.common.exception.ScenicSpotException;
import com.scenic.common.service.CartService;
import com.scenic.common.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    //添加
    @PostMapping("/{goodsId}")
    public RespBean insert(@PathVariable("goodsId") Integer goodsId, @RequestHeader("token") String token) throws ScenicSpotException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");
        //创建参数
        Cart cart = new Cart();
        cart.setUserId(userId);
        //cart.setGoodsId(goodsId);
        cartService.insertOrUpdate(cart);
        return RespBean.ok("添加到购物车成功~~");
    }
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id, @RequestHeader("token") String token) throws ScenicSpotException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");

        cartService.delete(id, userId);

        return RespBean.ok("删除成功");
    }
    @DeleteMapping
    public RespBean deleteByIds(@RequestBody Map<String , ArrayList<Integer>> data, @RequestHeader("token") String token) throws ScenicSpotException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");

        //获取被删除的购物车的id
        ArrayList<Integer> ids = data.get("ids");

        cartService.deleteByIds(ids, userId);

        return RespBean.ok("删除成功");
    }
    @PutMapping
    public RespBean update(@RequestBody Cart cart, @RequestHeader("token") String token) throws ScenicSpotException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");

        cartService.update(cart, userId);

        return RespBean.ok("修改购物车成功");
    }

    @GetMapping
    public RespBean selectCartList(@RequestHeader("token") String token) throws ScenicSpotException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");

        Cart cart = new Cart();
        cart.setUserId(userId);

        List<Cart> cartList = cartService.search(cart);

        return RespBean.ok("",cartList);
    }
    @GetMapping("/ids")
    public RespBean selectByIds(Integer[] cartIds, @RequestHeader("token") String token) throws ScenicSpotException {
        System.out.println(Arrays.toString(cartIds));
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");

        Cart cart = new Cart();
        cart.setUserId(userId);

        List<Cart> cartList = cartService.selectByIds(cartIds, userId);

        return RespBean.ok("",cartList);
    }
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id){
        Cart cart = cartService.selectById(id);
        return RespBean.ok("",cart);
    }
}
